package com.gress.shop.dao.impl;

import com.gress.shop.bo.LogInfo;
import com.gress.shop.bo.example.LogInfoExample;
import com.gress.shop.dao.ILogInfoDao;
import com.gress.shop.mapper.LogInfoMapper;
import com.gress.shop.query.LogQuery;
import com.gress.shop.util.HappyException;
import com.gress.shop.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created xiongwen with 2018/3/8 19:59
 * Description:
 */
@Service
public class LogInfoImpl implements ILogInfoDao {

    @Resource
    private LogInfoMapper logInfoMapper;


    @Override
    public int insertLogInfo(LogInfo info) {
        info.setId(UUIDUtil.getUUID());
        return logInfoMapper.insertSelective(info);
    }

    @Override
    public List<LogInfo> queryListLogInfo(LogInfoExample example) {
        return logInfoMapper.selectByExample(example);
    }

    @Override
    public List<LogInfo> listAll() {
        LogInfoExample example = new LogInfoExample();
        example.createCriteria().andIsValidEqualTo(1);
        example.setOrderByClause("create_time desc");
        return logInfoMapper.selectByExample(example);
    }

    @Override
    public LogInfo getById(String id) {
        LogInfoExample example = new LogInfoExample();
        example.createCriteria().andIsValidEqualTo(1).andIdEqualTo(id);
        List<LogInfo> logInfos = logInfoMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(logInfos)) {
            throw new HappyException("记录以丢失，请联系管理员恢复");
        }
        return logInfos.get(0);
    }

    @Override
    public int deleteById(String id) {
        if (1 != logInfoMapper.deleteByPrimaryKey(id)) {
            throw new HappyException("记录以丢失");
        }
        return 1;
    }

    @Override
    public List<LogInfo> query(LogQuery query) {
        return logInfoMapper.query(query);
    }

    @Override
    public int count(LogQuery logQuery) {
        return logInfoMapper.count(logQuery);
    }
}
